package com.naizhuai.ui.adapter;

/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.naizhuai.R;
import com.naizhuai.util.NaizhuAILog;

/**
 * This class is from the v7 samples of the Android SDK. It's not by me!
 * <p/>
 * See the license above for details.
 */
public class ManageDividerItemDecoration extends RecyclerView.ItemDecoration {

    private static final int[] ATTRS = new int[]{android.R.attr.listDivider};

    public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;

    public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;

    private float baseFloat=1f;

    private Drawable mDivider;

    //表示item之间的距离
    private int distance;

    private int cardDivideHeight;

    private int[] divideIndex;

    //是否画线
    private boolean drawLine;

    private int mOrientation;
    private int baseLineHeight;

    public ManageDividerItemDecoration(Context context, int orientation,int distance,boolean drawLine) {
        final TypedArray a = context.obtainStyledAttributes(ATTRS);
        mDivider = new ColorDrawable(context.getResources().getColor(R.color.item_divider_gray));
        this.distance=distance;
        cardDivideHeight=distance*6;
        this.drawLine=drawLine;
        a.recycle();
        setOrientation(orientation);
    }

    public void setOrientation(int orientation) {
        if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
            throw new IllegalArgumentException("invalid orientation");
        }
        mOrientation = orientation;
    }


    public float getBaseFloat() {
        return baseFloat;
    }

    public void setBaseFloat(float baseFloat) {
        this.baseFloat = baseFloat;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        if(!drawLine)
            return;
        if (mOrientation == VERTICAL_LIST) {
            drawVertical(c, parent);
        } else {
            drawHorizontal(c, parent);
        }
    }
    private final Rect mBounds = new Rect();
    public void drawVertical(Canvas c, RecyclerView parent) {
        c.save();
        final int left = parent.getPaddingLeft();
        final int right = parent.getWidth() - parent.getPaddingRight();
        final int childCount = parent.getChildCount();

        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            parent.getDecoratedBoundsWithMargins(child, mBounds);
            int index=parent.getChildAdapterPosition(child);
            if(containsElements(divideIndex,index)){
                final int top = child.getBottom() + params.bottomMargin+distance/2;
                final int bottom = top + cardDivideHeight;
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(c);
            }else{
                baseLineHeight=(int) (mDivider.getIntrinsicHeight() * baseFloat);
                baseLineHeight=baseLineHeight>0?baseLineHeight:1;
                final int top = child.getBottom() + params.bottomMargin+distance/2;
                final int bottom = top + baseLineHeight;
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(c);
            }
        }
        c.restore();

    }

    private boolean containsElements(int[] attrs,int index){
        if(attrs==null)
            return false;
        for (int in:attrs){
            if(in==index){
                return true;
            }
        }
        return false;
    }

    public void drawHorizontal(Canvas c, RecyclerView parent) {
        final int top = parent.getPaddingTop();
        final int bottom = parent.getHeight() - parent.getPaddingBottom();

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int left = child.getRight() + params.rightMargin;
            final int right = left + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    /**
     * 为我们的Item预留空间  因为我们的recyclerView默认item间是不留空间的
     *
     * @param outRect
     * @param view
     * @param parent
     * @param state
     */
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

        int itemPosition=parent.getChildAdapterPosition(view);
        if (mOrientation == VERTICAL_LIST) {
            if(containsElements(divideIndex,itemPosition)){
                outRect.set(0, 0,0, distance+cardDivideHeight);
            }else{
                outRect.set(0, 0, 0,distance+baseLineHeight);
            }
        } else {
            outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
        }
    }

    public int[] getDivideIndex() {
        return divideIndex;
    }

    public void setDivideIndex(int[] divideIndex) {
        this.divideIndex = divideIndex;
    }

    public void addDivideIndex(int divide) {
        if(divideIndex==null)
            divideIndex=new int[0];
        int sourceIndex=divideIndex.length;
        int[] newDivide = new int[divideIndex.length+1];
        System.arraycopy(divideIndex, 0, newDivide, 0, sourceIndex);
        newDivide[newDivide.length-1]=divide;
        divideIndex=newDivide;
    }



    public int getCardDivideHeight() {
        return cardDivideHeight;
    }

    public void setCardDivideHeight(int cardDivideHeight) {
        this.cardDivideHeight = cardDivideHeight;
    }
}
